package com.zqwm.fly.http;

import com.github.pagehelper.PageInfo;
import com.zqwm.fly.model.Article;
import com.zqwm.fly.service.ArticleService;
import com.zqwm.fly.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController
{
    @Autowired
    private ArticleService articleService;

    @RequestMapping("/")
    public String index(ModelMap map,@RequestParam(value="pageNum", defaultValue="1") int pageNum, @RequestParam(value="pageSize", defaultValue="10") int pageSize,String search)
    {
        Map<String,Object>mapParams = new HashMap<String,Object>();
        Article article = new Article();
        article.setTitle(search);
        PageInfo pageInfo = articleService.pageByParams(article,pageNum,pageSize);

        //文章分类
        List<Article>labelArtricle = articleService.getLabel();
        //文章归档
        //最新文章
        mapParams.put("orderParams","publishTime");
        List<Article> topnew = articleService.topArticle(mapParams);
        //热门总排行
        mapParams.put("orderParams","looksum");
        List<Article> topArticle = articleService.topArticle(mapParams);
        //今日热门
        mapParams.put("today", DateUtil.format(new Date(),"yyyy-MM-dd"));
        List<Article> topToday = articleService.topArticle(mapParams);
        map.addAttribute("page", pageInfo);
        map.addAttribute("search",search);
        map.addAttribute("topArticle",topArticle);
        map.addAttribute("topNew",topnew);
        map.addAttribute("topToday",topToday);
        map.addAttribute("labelArtricle",labelArtricle);
        System.out.println(pageInfo);
        return "ftl/index";
    }

    @RequestMapping("/reader")
    public String reader()
    {
        return "reader";
    }

}
